<!-- 全景看图 -->
<template>
  <div class="vrClass" @contextmenu.prevent="onContextmenu">
    <div ref="psvdbg" class="PSViewer"></div>
  </div>
</template>

<script>
import {Viewer} from "photo-sphere-viewer";
import "photo-sphere-viewer/dist/photo-sphere-viewer.css";
import {MarkersPlugin} from 'photo-sphere-viewer/dist/plugins/markers'
import 'photo-sphere-viewer/dist/plugins/markers.css'
import BaseView from './View';
import VRImg from '@/views/zutai/images/VR.png'
import uid from '@/views/zutai/api/zutai/uid.js'

export default {
  name: "ImageVR",
  extends: BaseView,
  data() {
    return {
      viewer: null,
      markersPlugin: null,
      VRImg: VRImg,
      location: {},
      selectMarkerId: null,
      bDeviceRealDataList: [],
    };
  },
  computed: {
    VRChange() {
      return this.detail.style.url;
    },
    VRMarkChange() {
      return this.detail.selectVRId;
    }
  },
  watch: {
    VRChange() {
      if (this.viewer) {
        this.destoryVR()
        //this.viewer.setPanorama(this.detail.style.url);
        this.init(this.detail.style.url, this.detail.style.markers);
      }
    },
    VRMarkChange(newVal, oldVal) {
      //移动视图到锚点
      let val = newVal.split('&');
      this.markersPlugin.gotoMarker(val[0], 1500)
      this.markersPlugin.updateMarker({
        id: val[0],
        tooltip: val[1],
        image: val[2],
      })
      this.markersPlugin.showAllTooltips()
    },
  },
  mounted() {
    this.init(this.detail.style.url, this.detail.style.markers);
    document.addEventListener('contextmenu', this.hideContextMenu)
    this.getRealDataList();
  },
  beforeDestroy() {
    this.destoryVR()
  },
  methods: {
    getRealDataList() {
      let url = "/zutai/bDeviceRealData/getBindDatalist"
      request({
        url: url,
        method: "get",
        params: this.queryParams,

      }).then((res) => {
        this.bDeviceRealDataList = res.rows;
      }).catch((error) => {
        // console.log(error)
      })
    },
    addMarker() {
      if (this.markersPlugin) {
        let marker = this.getMarkDeatil(this.location)
        this.markersPlugin.addMarker(marker)
        this.detail.style.markers.push(this.location)
      }
    },
    deleteMarker() {
      if (this.selectMarkerId && this.markersPlugin) {
        this.markersPlugin.removeMarker(this.selectMarkerId)
        this.detail.style.markers.forEach((element, index) => {
          if (element.id == this.selectMarkerId) {
            this.detail.style.markers.splice(index, 1);
            i--;
          }
        });
      }
    },
    onContextmenu(event) {
      this.$contextmenu({
        items: [{
          label: "增加锚点",
          icon: "el-icon-location-outline",
          onClick: () => {
            this.addMarker();
          }
        }, {
          label: "删除锚点",
          divided: true,
          icon: "el-icon-delete",
          onClick: () => {
            this.deleteMarker();
          }
        }, {
          label: "取消",
          icon: "el-icon-circle-close",
          onClick: () => {
          }
        },
        ],
        event, // 鼠标事件信息
        customClass: "custom-class", // 自定义菜单 class
        zIndex: 9999, // 菜单样式 z-index
        minWidth: 230 // 主菜单最小宽度
      });

      return true;
    },
    destoryVR() {
      if (this.viewer) try {
        this.viewer.destroy()
        console.log("VR视图已销毁");
      } catch (e) {
        console.log(e)
        const viewer = this_.$refs.psvdbg
        viewer.removeChild(viewer.childNodes[0])
        console.log("VR视图已销毁");
      }
    },
    hideContextMenu(event) {
      console.log('event', event);
    },
    init(url, markers) {
      console.log('markers', markers);
      let this_ = this;
      let initMarks = [];
      markers.forEach(element => {
        initMarks.push(this.getMarkDeatil(element))
      });
      // console.log('initMarks', initMarks);
      this.viewer = new Viewer({
        panorama: url, // 图片路径
        container: this_.$refs.psvdbg, // 容器
        caption: '<b style="margin-left:-30vw;">' + this.detail.name + '</b>',//说明文字
        //autorotateDelay: 1000,//自动旋转延迟
        navbar: true,//是否展示工具栏
        size: {
          width: "100%",
          height: "100%",
        },
        plugins: [
          [MarkersPlugin, {markers: initMarks,}],
        ],
        navbar: {
          maxFov: 100
        }
      });
      this.markersPlugin = this.viewer.getPlugin(MarkersPlugin)
      if (this.editMode) {
        this.markersPlugin.on('over-marker', (e, marker) => {
          console.log(`Cursor is over marker ${marker.id}`);
        });
        this.markersPlugin.on('select-marker', (e, marker) => {
          console.log(`Cursor is select marker ${marker.id}`);
          this.selectMarkerId = marker.id
        });
        this.viewer.on('click', (e, data) => {
          console.log(`${data.rightclick ? 'right ' : ''}clicked at longitude: ${data.longitude} latitude: ${data.latitude}`);
          this.location = {
            longitude: data.longitude,
            latitude: data.latitude,
            id: uid(),
            dataBind: {
              paramField: '',
              acquisitionFormula: '',
              writeFormula: '',
              imei: '',
              paramUnit: '',
              paramName: '',
            }
          }
        });
      } else {
        this.markersPlugin.on('over-marker', (e, marker) => {
          console.log(`Cursor is select marker ${marker.id}`);
          let msg = sessionStorage.getItem('ps-marker-' + marker.id)
          if (msg) {
            this.$notify({
              title: '设备变量',
              message: msg,
              type: 'success',
              duration: 3000
            });
          } else {
            this.$notify({
              title: '设备变量',
              message: '无绑定设备变量',
              type: 'warning',
              duration: 3000
            });
          }

        });
      }
      this.viewer.once('ready', () => {
        console.log(`viewer is ready`);
        this.markersPlugin.showAllTooltips();
      });
    },
    getMarkDeatil(data) {
      let imageUrl = data.vrUrl == null ? VRImg : data.vrUrl
      let marker = {
        "id": data.id,
        "status": 1,
        "anchor": "center center",
        image: imageUrl,
        width: 50,
        height: 50,
        latitude: data.latitude,
        longitude: data.longitude,
        visible: true,
        type: "marker",
        tooltip: data.dataBind.paramName
      }
      return marker;
    },
  },
};
</script>
<style>
.vrClass {
  height: 100%;
  width: 100%;
}

.custom-tooltip {
  max-width: none;
  width: 300px;
  padding: 0;
  background-color: aliceblue;
  text-align: center;
  box-shadow: 0 0 0 3px rgb(209, 201, 201);
}

.custom-tooltip img {
  width: 100%;
  border-radius: 4px 4px 0 0;
}

.custom-tooltip h5 {
  text-align: center;
}

.custom-tooltip p {
  margin: 1rem;
  text-align: center;
}

.custom-tooltip span {
  color: rgb(138, 130, 130);
  font-size: 16px;
}

.j-tooltip {
  position: relative;
  border-bottom: 1px dotted black;
}

.j-tooltip:before {
  /* content: "title"; */
  position: absolute;
  width: 150px;
  background-color: #062b45;
  color: white;
  text-align: center;
  padding: 10px;
  line-height: 1.2;
  border-radius: 6px;
  z-index: 1;
  opacity: 0;
  transition: opacity 0.6s;
  bottom: 125%;
  left: 50%;
  margin-left: -60px;
  font-size: 0.75em;
  visibility: hidden;
  word-break: break-all;
}

.j-tooltip:hover:before {
  opacity: 1;
  visibility: visible;
}</style>
